package vcp.gui.forms;

import javax.swing.JPanel;

import vcp.gui.components.VCPPanel;
import vcp.gui.forms.validators.NumericValidator;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextArea;
import javax.swing.JButton;

import control.OrderController;
import control.SimpleClient;
import db.parking.beans.Complaints;
import db.parking.beans.Orders;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ComplaintForm extends VCPPanel {
	private final JLabel lblComplaintFormError;
	private JTextField textOOID;
	private JTextField textOTitle;
	private SimpleClient client;
	private JTextArea textAreaODescr;

	/**
	 * Create the panel.
	 */
	public ComplaintForm(SimpleClient client) {

		JLabel lblOrderID = new JLabel("OrderID");
		lblComplaintFormError = new JLabel("");
		lblComplaintFormError.setForeground(Color.RED);
		this.client = client;

		textOOID = new JTextField();
		textOOID.setColumns(10);

		JLabel lblCtitle = new JLabel("Title");

		textOTitle = new JTextField();
		textOTitle.setColumns(10);

		JLabel lblOdescr = new JLabel("Description");

		JButton btnSend = new JButton("send");
		btnSend.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				insertComplaint(e);

			}
		});

		textAreaODescr = new JTextArea();

		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout
				.setHorizontalGroup(groupLayout
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								groupLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												groupLayout
														.createParallelGroup(
																Alignment.TRAILING)
														.addGroup(
																groupLayout
																		.createSequentialGroup()
																		.addGap(12)
																		.addGroup(
																				groupLayout
																						.createParallelGroup(
																								Alignment.LEADING)
																						.addGroup(
																								groupLayout
																										.createSequentialGroup()
																										.addComponent(
																												lblOdescr)
																										.addGap(29)
																										.addComponent(
																												textAreaODescr,
																												GroupLayout.DEFAULT_SIZE,
																												250,
																												Short.MAX_VALUE))
																						.addGroup(
																								groupLayout
																										.createSequentialGroup()
																										.addGroup(
																												groupLayout
																														.createParallelGroup(
																																Alignment.LEADING)
																														.addComponent(
																																lblOrderID)
																														.addComponent(
																																lblCtitle))
																										.addGap(43)
																										.addGroup(
																												groupLayout
																														.createParallelGroup(
																																Alignment.LEADING)
																														.addComponent(
																																textOOID,
																																GroupLayout.DEFAULT_SIZE,
																																260,
																																Short.MAX_VALUE)
																														.addComponent(
																																textOTitle,
																																260,
																																260,
																																260)))
																						.addComponent(
																								btnSend))
																		.addContainerGap(
																				80,
																				GroupLayout.PREFERRED_SIZE))
														.addGroup(
																groupLayout
																		.createSequentialGroup()
																		.addComponent(
																				lblComplaintFormError)
																		.addContainerGap(
																				444,
																				Short.MAX_VALUE)))));
		groupLayout
				.setVerticalGroup(groupLayout
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								groupLayout
										.createSequentialGroup()
										.addGap(8)
										.addComponent(lblComplaintFormError)
										.addPreferredGap(
												ComponentPlacement.UNRELATED)
										.addGroup(
												groupLayout
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																textOOID,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																lblOrderID))
										.addPreferredGap(
												ComponentPlacement.UNRELATED)
										.addGroup(
												groupLayout
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																textOTitle,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(lblCtitle))
										.addGap(14)
										.addGroup(
												groupLayout
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																textAreaODescr,
																GroupLayout.PREFERRED_SIZE,
																84,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(lblOdescr))
										.addPreferredGap(
												ComponentPlacement.UNRELATED)
										.addComponent(btnSend)
										.addContainerGap(73, Short.MAX_VALUE)));
		setLayout(groupLayout);

	}

	public void insertComplaint(ActionEvent e) {
		String orderId = textOOID.getText();
		String title = textOTitle.getText();
		String description = textAreaODescr.getText();
		OrderController orderControl; // cretae order controller
		Complaints bean; // complaint to send

		NumericValidator verifyID = new NumericValidator(); // create validator
		if (!verifyID.validate(orderId)) { // wrong input
			JOptionPane.showMessageDialog(null,
					"Invalid ID Entered,Please Re-Enter",
					"Invalid Input Error", JOptionPane.ERROR_MESSAGE);
		} else {
			try {
				bean = new Complaints();
				bean.setDescription(description);
				bean.setOrderID(Integer.parseInt(orderId));
				bean.setTitle(title);
				orderControl = new OrderController(client);
				orderControl.SubmitComplaint(bean);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
	}
}